<!-- BEGIN PAGE HEADER-->
<?php $this->load->view('partials/breadcrumb.tpl') ?>
<style>
    .flex-housing {
        display: flex;
        justify-content: space-around;
        text-align: center;
        align-items: center;
        flex-wrap: wrap;
    }

    .housing-item {
        width: 23%;
        margin-top: 10px;
    }

    .note-danger {
        margin-bottom: 0;
    }

</style>
<div class="row">
    <div class="col-xs-12 ">
        <!-- BEGIN SAMPLE FORM PORTLET-->
        <div class="portlet box green ">
            <?= edit_form_caption('添加出库记录') ?>
            <div class="portlet-body form">
                <?= form_open('', Array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_edit', 'data-category-id' => $obj->id)) ?>
                <div class="form-body">
                    <input type="hidden" name="type" value="0">
                    <?= edit_form_dropdown('shop_id', array('' => '选择账户组...') + $allShops, set_value('shop_id', $obj->shop_id), '所属账户组', "class='select2me' style='width:100%;'") ?>

                    <div class="form-group">
                        <label class="col-xs-3 control-label">可出库批次</label>
                        <div class="col-xs-7 flex-housing">
                            <div class="col-xs-12" style="display: flex;justify-content: space-around;">
                                <h4>商品单价：<span id="single-price" style="color: #ff3761;"><?= $price ?></span></h4>
                                <h4>数量总计：<span id="out-total" style="color: #ff3761;"></span></h4>
                                <h4>价格总计：<span id="out-total-amount" style="color: #ff3761;"></span></h4>
                            </div>
                            <?php foreach ($housings as $v): ?>
                                <div class="housing-item">
                                    <input type="text" name="amount[<?= $v->warehousing ?>]" class="form-control amount-total" onblur="getAmountChange(this)" placeholder="总价">
                                    <div class="note note-danger">
                                        <h4 class="block" style="padding: 0"><?= $v->warehousing ?></h4>
                                        <p><?= IOSS_StaticConfig::getShippingWarehouse($v->location) . '库：' . IOSS_StaticConfig::getShippingBlock($v->area) ?></p>
                                        <p>当前剩余数量：<span class="label label-success"
                                                        style="font-size: 13px"><?= $v->left_number ?></span></p>
                                    </div>
                                    <input onblur="getChange(this);" name="housing[<?= $v->warehousing ?>]" type="number" class="form-control out-status" placeholder="请输入该批次出库数量">
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
<!--                    <input type="hidden" name="amount">-->
                    <?= edit_form_input('related_person', set_value('related_person', $obj->related_person), '负责人') ?>
                    <?= edit_form_dropdown('type_mark', array('' => '选择出库类型...') + IOSS_StaticConfig::getShippingOutType(), set_value('type_mark', $obj->type_mark), '出库类型', "id='mine_se'") ?>
                    <div id="order-no" style="display: none;">
                        <?= edit_form_input('order_no', set_value('order_no', $obj->order_no), '订单号（自发货）') ?>
                    </div>
                    <?= edit_form_radio_list('status', MStorageRecord::getOutState(), set_value('status', ($thisMethod == 'edit' ? $obj->status : '1')), '出入库状态') ?>

                    <div class="form-group">
                        <label class="col-xs-3 control-label">出库时间</label>
                        <div class="col-xs-7">
                            <div class="input-group input-large date-picker input-daterange" data-date=""
                                 data-date-format="yyyy-mm-dd">
                                <input type="text" class="form-control" name="purchase_time"
                                       value="<?= set_value('purchase_time', $obj->purchase_time) ?>" placeholder="出库时间"
                                       autoComplete="off">
                            </div>
                        </div>
                    </div>
                    <?= edit_form_textarea('remark', set_value('remark', $obj->remark), '备注信息') ?>
                </div>
                <div class="form-actions">
                    <div class="row">
                        <div class="col-xs-offset-3 col-xs-4">
                            <button type="button" id="add-in-submit" class="btn green">
                                <span class="md-click-circle md-click-animate"></span>保存
                            </button>
                            <a href="<?= site_url($thisModule . 'product') ?>" class="btn default">返回</a>
                        </div>
                    </div>
                </div>
                <?= form_close() ?>
            </div>
        </div>
    </div>
</div>
<link href="<?= image_url('/static/assets/global/plugins/jstree/dist/themes/default/style.min.css') ?>" rel="stylesheet"
      type="text/css"/>
<script type="text/javascript">

    $("#add-in-submit").click(function () {
        var p_time = $("input[name='purchase_time']").val();
        if (!p_time){
            toastr.error('请选择出库时间！');
            return false;
        }else {
            $("#form_edit").submit();
        }
    });

    function getAmountChange(e) {
        var outArr = $(".amount-total");
        var num = 0;
        for (var i = 0; i < outArr.length; i++) {
            if (outArr[i].value){
                num += parseInt(outArr[i].value);
            }
        }
        $("#out-total-amount").text(num.toFixed(3));
    }
    function getChange(e) {
        var outArr = $(".out-status");
        var num = 0;
        for (var i = 0; i < outArr.length; i++) {
            if (outArr[i].value){
                num += parseInt(outArr[i].value);
            }
        }
        $("#out-total").text(num);
        var amount = num * <?= $price ?>;
        $("#out-total-amount").text(amount.toFixed(3));
        var thisValue = $(e).val() * <?= $price ?>;
        $(e).siblings('.amount-total').val(thisValue.toFixed(3));
    }

    $(document).ready(function () {
        $("#warehousing-merge").click(function () {
            var ship = $("select[name='product_id']").val();
            console.log(ship);
            if (!ship) {
                toastr.error('请先选择要入库的商品！', '失败');
                return;
            }

            $.ajax({
                url: "<?= site_url('/storage/warehouse/ajaxWarehousing') ?>",
                method: 'POST',
                dataType: 'JSON',
                data: {'product_id': ship},
                success: function (res) {
                    if (res.success) {
                        $("#warehousing-time").toggle('fade');
                        var html = '';
                        for (var i = 0; i < res.data.length; i++) {
                            html += '<option value="' + res.data[i].warehousing + '">' + res.data[i].warehousing + '</option>'
                        }
                        console.log(html);
                        $("select[name='warehousing']").append(html);
                    } else {
                        toastr.error('当前产品暂无入库批次记录！', '失败');
                        return;
                    }
                },
                fail: function (res) {
                    console.log(res);
                }
            });

        });

        $("#housing").change(function () {
            $("#warehousing").html(this.value);
            $("input[name='warehousing']").val(this.value);
        });

        $('#mine_se').change(function () {
            var choose_value = $('#mine_se').val();
            if (choose_value == 7) {
                $("#order-no").fadeIn();
            } else {
                $("#order-no").fadeOut();
            }
        });

        $("select[name='type']").change(function () {
            var type = $("select[name='type']").val();
            console.log(typeof (type));
            console.log(type);
            if (parseInt(type)) {
                console.log('入库');
                $('#warehousing-time').fadeOut();
                $('.change-housing').fadeIn();
            } else if (parseInt(type) == 0) {
                if (!$("input[name='product_id']").val()) {
                    toastr.error('请先选择要出库的商品！', '失败');
                    return;
                }
                console.log('出库');
                $('.change-housing').fadeOut();
                $('#warehousing-time').fadeIn();
            }
        });
    })
</script>
